﻿using System;
using System.Collections;
using System.Collections.Generic;
using static SimpleTerminal.TerminalControl.TerminalContainer;

namespace SimpleTerminal.TerminalControl
{
    public class Line
    {
        public int MaxOffset = 0;
        public List<Character> Characters = new List<Character>();

        public Character endCharacter;

        private readonly TerminalContainer rowsContainer;

        public int State = CommonDefine.PAINT_INVALID;

        public Line(TerminalContainer rowsContainer)
        {
            this.rowsContainer = rowsContainer;
            endCharacter = new Character()
            {
                fg = rowsContainer.defaultForeground,
                bg = rowsContainer.defaultBackground,
                mask = CharacterMask.None,
                c = ' ',
            };
        }
        public struct Truecolour {
            public TerminalColor fg;//前景色
            public TerminalColor bg;//背景色  
        }

        public class Character
        {
            public char c;//字符
            public byte mask; //掩码标记
            public TerminalColor fg;//前景色
            public TerminalColor bg;//背景色  
            //public byte mask = CharacterMask.None; //掩码标记
            //public TerminalColor foreground = null;//前景色
            //public TerminalColor background = null;//背景色

            public TerminalColor Fg
            {
                get
                {
                    return fg;
                }
            }

            public TerminalColor Bg
            {
                get
                {
                    return bg;
                }
            }


            public bool TestMask(byte testmask)
            {
                return (mask & testmask) == testmask;
            }


        }

        public  Character GetCharacter(int index)
        {
            if (index >= Characters.Count)
            {
                return  endCharacter;
            }
            return  Characters[index];
        }


        public void Delete()
        {

        }

        public void Clear()
        {
            endCharacter.bg = rowsContainer.defaultBackground;
            endCharacter.fg = rowsContainer.defaultForeground;
            Characters.Clear();
        }

        /// <summary>
        /// 调整行的实际字符元素数
        /// </summary>
        /// <param name="index">调整到第几个元素（0为起始索引）</param>
        /// <param name="shrink">是否允许缩小</param>
        public void Adjustment(int index, bool shrink)
        {
            if (Characters.Count <= index)
            {
                do
                {
                    Character character = new Character()
                    {
                        mask = CharacterMask.Empty,
                        c = ' ',
                        bg = endCharacter.bg,
                        fg = endCharacter.fg,
                    };
                    Characters.Add(character);
                } while (Characters.Count <= index);
            }
            else if (shrink && index < 0)
            {
                Characters.Clear();
            }
            else if (shrink && Characters.Count > index + 1)
            {
                Characters.RemoveRange(index + 1, Characters.Count - index - 1);
            }

        }
        public Character this[int index]
        {
            get
            {
                index = Math.Max(index, 0);
                Adjustment(index, false);
                return Characters[index];
            }

        }

        public override string ToString()
        {
            char[] buffer = new char[Characters.Count];
            int j = 0;
            for (int i = 0; i < Characters.Count; i++)
            {
                Character character = this[i];
                if (character.TestMask(CharacterMask.Half2))
                    continue;
                buffer[j] = character.c;
                j++;
            }
            return new string(buffer, 0, j);
        }
    }
}
